from app.models.database import db
from datetime import datetime

class ApiInfo(db.Model):
    """API信息模型"""
    __tablename__ = 'api_info'
    
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)
    method = db.Column(db.String(10), nullable=False)
    path = db.Column(db.String(255), nullable=False)
    description = db.Column(db.Text)
    headers = db.Column(db.JSON)
    params = db.Column(db.JSON)
    body = db.Column(db.JSON)
    response = db.Column(db.JSON)
    tags = db.Column(db.JSON)  # 存储标签列表
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    
    # 关联到场景步骤
    steps = db.relationship('SceneStep', back_populates='api', foreign_keys='SceneStep.api_id')
    
    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'method': self.method,
            'path': self.path,
            'description': self.description,
            'headers': self.headers or {},
            'params': self.params or {},
            'body': self.body or {},
            'response': self.response or {},
            'tags': self.tags or [],
            'created_at': self.created_at.strftime('%Y-%m-%d %H:%M:%S'),
            'updated_at': self.updated_at.strftime('%Y-%m-%d %H:%M:%S')
        }